<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: process.class.php 97083 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php

defined('PHPFOX') or exit('NO DICE!');

class Follow_Service_Process extends Phpfox_Service
{
    public function __construct()
    {
        $this->_sTable = phpfox::gett('user');
    }
    
    public function getUserIdByName($sName)
    {
        return $this->database()->select('*')   
                                ->from($this->_sTable)
                                ->where('user_name = "'.$this->database()->escape($sName).'"')
                                ->execute('getSlaveRow');
    }
    
    public function hasSetting($iId, $sType = 'user')
    {
        return $this->database()->select('*')
                ->from(Phpfox::getT('follow_setting'),'fs')
                ->where('fs.item_id = '.(int)$iId. ' AND fs.type_id = "'.$this->database()->escape($sType).'"')
                ->execute('getSlaveRow');
    }
    
    public function getSetting($iId, $sType = 'user')
    {
        if($aRow = $this->hasSetting($iId, $sType))
        {
            return $aRow;
        }
        else
        {
            if($aRow = $this->addSetting($iId, $sType))
            {
                return $aRow;
            }
            else
            {
                return false;
            }
        }
    }
    
    public function changeApproval($iId, $iStatus, $sType = 'user')
    {
        return $this->database()->update(Phpfox::getT('follow_setting'), array('auto_approval' => $iStatus), 'type_id = "'.$this->database()->escape($sType).'" AND item_id = '.(int)$iId);    
    }
    
    public function addSetting($iId, $sType = 'user')
    {
        $aInsert = array(
            'item_id' => $iId,
            'time_stamp' => PHPFOX_TIME,
            'type_id' =>$sType,
        );
        $iId = $this->database()->insert(Phpfox::getT('follow_setting'), $aInsert);
        return $this->database()->select('*')
            ->from(Phpfox::getT('follow_setting'),'fs')
            ->where('fs.setting_id = '.(int)$iId. ' AND fs.type_id = "'.$this->database()->escape($sType).'"')
            ->execute('getSlaveRow');
    }
    public function isPending($iId, $sType = 'user')
    {
        return $this->database()->select('count(*)')
                ->from(Phpfox::getT('follow_pending'), 'fp')
                ->where('fp.user_id ='.(int)Phpfox::getUserId() .' AND fp.item_id = '.(int)$iId .' AND fp.type_id = "'.$this->database()->escape($sType).'"')
                ->execute('getSlaveField');
    }
}
?>
